package com.rmdjzz.slmmn.controller.user.userManagement;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rmdjzz.slmmn.pojo.base.User;
import com.rmdjzz.slmmn.pojo.result.Result;
import com.rmdjzz.slmmn.pojo.vo.UserLogionVo;
import com.rmdjzz.slmmn.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/*@创建时间   2023/6/5
  * @作者 lzy
   * @内容 用户的登录，注册
 */

@Controller
@Api(tags = "用户登录")
@RequestMapping("/user")

public class UserManagement {
    @Autowired
    private UserService userService;

    @GetMapping("/userLoginIndex")
    @ApiOperation("登录首页")
    public String getUserLoginIndex() {
        return "login";
    }

    @GetMapping("/userRegisterIndex")
    @ApiOperation("注册首页")
    public String   getUserRegisterIndex() {
        return "register";
    }

    @GetMapping("/userLoginAccount/{account}")
    @ApiOperation("用户登录/查找账号")
    @ResponseBody
    public Result userLoginAccount(@PathVariable String account){
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("account",account);
        try{
            if(!userService.getOne(userQueryWrapper).equals("")&&!userService.getOne(userQueryWrapper).equals("null")){
                return Result.ok("成功");
            }else{
                return Result.ok("该账号不存在");
            }
        }catch(Exception e){
            return Result.ok("该账号不存在");
        }

    }

    @GetMapping("/userLoginPassword")
    @ApiOperation("用户登录/验证账号，密码")
    @ResponseBody
    public Result userLoginPassword(UserLogionVo userLogionVo, HttpSession session){
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("account",userLogionVo.getAccount()).eq("password",userLogionVo.getPassword());
        try{
            if(!userService.getOne(userQueryWrapper).equals("")&&!userService.getOne(userQueryWrapper).equals("null")){
                QueryWrapper<User> qw = new QueryWrapper<>();
                qw.eq("account",userLogionVo.getAccount());
                session.setAttribute("user",userService.getOne(qw));
                return Result.ok("成功");
            }else{
                throw  new RuntimeException("密码错误！");
            }
        }catch(Exception e){
            return Result.ok("密码错误!");
        }
    }

    @GetMapping("/userRegister")
    @ApiOperation("用户注册")
    @ResponseBody
    public Result userRegister(User user){
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("account",user.getAccount());
        try{
            if(userService.getOne(userQueryWrapper).equals("")&&userService.getOne(userQueryWrapper).equals("null")){
                return Result.ok("该账号已存在");
            }else{
                return Result.ok("该账号已存在");
            }
        }catch(Exception e){
            userService.save(user);
            return Result.ok("成功");
        }

    }
}
